package com.fyy.netty.chapter02.c2;

import io.netty.buffer.ByteBuf;
import io.netty.buffer.ByteBufAllocator;

/**
 * @Author fyy
 * @Description
 * @Date 2021/5/6 14:07
 */
public class ByteBufTest {

    public static void main(String[] args) {
        //创建ByteBuf
        ByteBuf buf = ByteBufAllocator.DEFAULT.buffer(); //默认大小为256
        //自动扩容测试
        System.out.println(buf); //打印初始对象
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < 300; i++) {
            sb.append("a");
        }
        buf.writeBytes(sb.toString().getBytes());
        System.out.println(buf); //打印填入数据后对象

        //创建池化基于堆的ByteBuf
        ByteBuf buf1 = ByteBufAllocator.DEFAULT.heapBuffer(10);
        //创建池化基于直接内存的ByteBuf
        ByteBuf buf2 = ByteBufAllocator.DEFAULT.directBuffer(10);

        byte b = buf.readByte(); //每次读取一个字节
        boolean b1 = buf.readBoolean(); //读取Boolean值

        buf.markReaderIndex(); //添加标记
        buf.readByte();
        buf.resetReaderIndex(); //重置到标记点

        buf.getByte(1); //获取下标为1的字节
        buf.getBoolean(1); //获取下标为1的字节并封装成Boolean

        buf.retain(); //计数加一
        buf.release(); //计数减一


    }

}
